{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example showing how to programmatically toggle the Keras backend from Theano to Tensorflow and back to Theano."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using Theano backend.\n"
     ]
    }
   ],
   "source": [
    "import logging\n",
    "logging.basicConfig(format=\"%(asctime)s: %(message)s\", level=logging.INFO, datefmt=\"%H:%M:%S\")\n",
    "\n",
    "import numpy\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense\n",
    "from utils import keras_backend_utils as kbu\n",
    "\n",
    "seed = 7\n",
    "numpy.random.seed(seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "09:55:03: Current backend : theano\n",
      "09:55:03: Toggling the backend ...\n",
      "09:55:03: Keras version : 1.0.5\n",
      "09:55:03: Reading /root/.keras/keras.json ...\n",
      "09:55:03: Toggling from 'theano' to 'tensorflow'\n",
      "09:55:03: Writing to /root/.keras/keras.json ...\n",
      "Using TensorFlow backend.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'backend': 'theano',\n",
      " 'epsilon': 1e-07,\n",
      " 'floatx': 'float32',\n",
      " 'image_dim_ordering': 'th'}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "09:55:06: Current backend : tensorflow\n"
     ]
    }
   ],
   "source": [
    "# do some work with Theano, \n",
    "from keras import backend as K\n",
    "logging.info(\"Current backend : {}\".format(K._BACKEND))\n",
    "logging.info(\"Toggling the backend ...\")\n",
    "\n",
    "#then toggle to using Tensorflow as a backend\n",
    "kbu.toggle_keras_backend()\n",
    "logging.info(\"Current backend : {}\".format(K._BACKEND))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "09:55:12: Toggling the backend the 2nd time ...\n",
      "09:55:12: Keras version : 1.0.5\n",
      "09:55:12: Reading /root/.keras/keras.json ...\n",
      "09:55:12: Toggling from 'tensorflow' to 'theano'\n",
      "09:55:12: Writing to /root/.keras/keras.json ...\n",
      "Using Theano backend.\n",
      "09:55:12: Current backend : theano\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'backend': 'tensorflow',\n",
      " 'epsilon': 1e-07,\n",
      " 'floatx': 'float32',\n",
      " 'image_dim_ordering': 'th'}\n"
     ]
    }
   ],
   "source": [
    "# Toggle back to Theano\n",
    "logging.info(\"Toggling the backend the 2nd time ...\")\n",
    "kbu.toggle_keras_backend()\n",
    "logging.info(\"Current backend : {}\".format(K._BACKEND))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
